package com.xiaoge.system.controller;


import com.alibaba.fastjson.JSON;
import com.xiaoge.response.Result;
import com.xiaoge.system.entity.Blog;
import com.xiaoge.system.service.BlogService;
import com.xiaoge.system.utils.elasticSearchUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoge
 * @since 2021-04-13
 */
@Api("我的博客")
@RestController
@RequestMapping("/system/blog")
@CrossOrigin
public class BlogController {

    @Autowired
    public elasticSearchUtils elasticSearchUtils;
    @Autowired
    public BlogService blogService;
    /**
     * 保存文件生成的blog
     */
    @PostMapping("/saveBlog")
    @ApiOperation("保存文件生成的blog")
    public Result saveBlog(@RequestBody String blog, HttpServletRequest req) throws IOException {
        String username = (String) req.getSession().getAttribute("username");
        String userid = (String) req.getSession().getAttribute("userid");
        UUID uuid = UUID.randomUUID();
        Map<String,Object> parse = (Map<String, Object>) JSON.parse(blog);
        Map<String,Object> parse1 = (Map<String, Object>) JSON.parse(parse.get("blog").toString());
        String title = (String) parse1.get("title_bl");
        String context = (String) parse1.get("context");
        String description = (String) parse1.get("description");
        String category = (String) parse1.get("category");
        String path = "D:\\test\\file_blog\\";
        String s = context;
        FileWriter fw = null;
        File f = new File(path+uuid+".md");

        try {
            fw = new FileWriter(f);
            BufferedWriter out = new BufferedWriter(fw);
            out.write(s, 0, s.length());
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        Blog blogEntity = new Blog();
        blogEntity.setTitle(title);
        blogEntity.setIntroduction(description);
        blogEntity.setFilepath(path+uuid+".md");
        blogEntity.setUsername(username);
        blogEntity.setUserId(userid);
        blogEntity.setCategory(category);
        blogService.insertBlog(blogEntity);

        /**
         * es插入数据
         */
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("title",title);
        map.put("description",description);
        map.put("username",username);
        map.put("creatime",new Date());
        map.put("category",category);
        map.put("context",blog);
        list.add(map);
        elasticSearchUtils.btcInsertIndexContext(list,"blog");

        return Result.ok();

    }

    /**
     * 查询es的解雇，根据关键字
     */
    @PostMapping("/queryEsByKeywords")
    public Result queryEsByKeywords(@RequestParam String keywords,
                                    @RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize
                                   ) throws IOException {
        List<Map<String, Object>> blogs = elasticSearchUtils.searchByKeyword(keywords, pageNum, pageSize, "blog");
        return Result.ok().data("records",blogs);
    }

    /**
     * 获取所有分类
     */
    @PostMapping("/queryAllCategory")
    public Result queryAllCategory(){
        List<Map<String, Object>> allCategory = blogService.queryAllCategory();
        return Result.ok().data("records",allCategory);
    }
}

